package com.cbs.android.module.user.user.module;

import com.cbs.android.component.applicationutils.Global;
import com.cbs.android.component.applicationutils.network.CBSRequest;
import com.cbs.android.component.applicationutils.network.ErrorResponse;
import com.cbs.android.component.network.Request;
import com.cbs.android.component.network.SimpleResponseHandler;
import com.cbs.android.module.user.user.UserModule;
import com.cbs.android.module.user.user.entity.User;
import com.cbs.android.module.user.user.handler.ModuleHandler;
import com.google.gson.Gson;

/**
 * Created by broche on 5/10/16.
 */
public class UserModuleVerifyCode {
    private static final String UrlTpl = "%s/verify/api/v1/code?code=%s&phone=%s";
    private String phone;
    private String verifyCode;
    private boolean needCheck;
    private ModuleHandler moduleHandler;

    public UserModuleVerifyCode(String phone, String verifyCode, boolean needCheck, ModuleHandler moduleHandler) {
        this.phone = phone;
        this.verifyCode = verifyCode;
        this.needCheck = needCheck;
        this.moduleHandler = moduleHandler;
    }

    public void start() {
        if (needCheck) {
            if (UserModule.getUser() == null) {
                if (moduleHandler != null) {
                    ErrorResponse errorResponse = new ErrorResponse();
                    errorResponse.setErrorNo(99999);
                    errorResponse.setErrorMsg("请先登录您的账号");
                    moduleHandler.onFailure(999, new Gson().toJson(errorResponse));
                }
                return;
            }
            User user = UserModule.getUser();
            if (!user.getUserName().equals(phone)) {
                if (moduleHandler != null) {
                    ErrorResponse errorResponse = new ErrorResponse();
                    errorResponse.setErrorNo(99998);
                    errorResponse.setErrorMsg("用户名不匹配");
                    moduleHandler.onFailure(999, new Gson().toJson(errorResponse));
                }
                return;
            }
        }

        Request request = new CBSRequest(String.format(UrlTpl, Global.getServerHost(), verifyCode, phone))
                .setResponseHandler(new SimpleResponseHandler() {
                    @Override
                    public void onSuccess(Object entity) {
                        if (moduleHandler != null) {
                            moduleHandler.onSuccess(entity);
                        }
                    }

                    @Override
                    public void onFailure(int code, String message) {
                        if (moduleHandler != null) {
                            moduleHandler.onFailure(code, message);
                        }
                    }

                    @Override
                    public void onException(Request request, Exception e) {
                        if (moduleHandler != null) {
                            moduleHandler.onException(e);
                        }
                    }
                });
        UserModule.getHttpClient().send(request);
    }
}
